package com.dangdang.sharding.dp.filter.spring.core;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.dangdang.sharding.dp.filter.dp.service.Controller;
import com.dangdang.sharding.dp.filter.spring.proxy.FilterCglibProxy;

public class FilterBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
	private ApplicationContext applicationContext;

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		
		if(bean instanceof Controller) {
			Controller controller = (Controller) bean;
			return new FilterCglibProxy(controller,applicationContext).getProxyInstance();
		}
		return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

}
